--力重复精度测试 
import("System")
import("System.IO.Ports")
import("Jodell.Tool","Jodell.Tool")
--打开串口
COMID = "COM9"
slave_ID = 4
test_I_ref = 1000
client=Lua_ModbusRtuClient()
client.PortName=COMID
client:Connect()
-- 创建 CSV 对象并打开
Info(slave_ID)
Info(client)
ret,SN = client:ReadString(slave_ID,562,16)
index=string.find(SN,"\0")
SN=string.sub(SN,1,index-1)
Info(string.format("设备编号%s",SN))
localpath = string.format("./ForceCtrl\\推力测试(%s).csv",SN)
Info(localpath)
writer = Lua_CsvWriter(localpath)
writer:Open()
writer:WriteString("参考电流(A)")
writer:WriteSplitter()
writer:WriteString("反馈电流(A)")
writer:WriteSplitter()
writer:WriteString("测量压力(N)")
writer:WriteSplitter()
writer:WriteString("设置电流(mA)")
writer:WriteSplitter()
writer:WriteLine()

--设备使能
ret = client:WriteUInt16(slave_ID,1002,1)	
if ret ~= 0 then
	Error("设置设备使能失败")
	goto exit
end
--设备复位
ret = client:WriteUInt16(slave_ID,1004,1)	
if ret ~= 0 then
	Error("设置设备复位失败")
	goto exit
end
--切换到标定模式
ret = client:WriteUInt16(slave_ID,0x013d,1)
if ret ~= 0 then
	Error("设置标定模式失败")
	goto exit
end
Delay(100)

ret = client:WriteUInt16(slave_ID,1010,1000)	
	if ret == 0 then
		Info(string.format("设置拉力:%dmA",1000))
	else
		Error(string.format("设置拉电流:%dmA失败",1000))
		goto exit
	end

--开始测试
for Tick = 0, 100, 1 do

	--终止测试
    	if (IsCancel) then
    		Warn("手动终止")
            goto exit
		break
	end
	--读取运行状态
	--ret,runstatus = client:ReadUInt16(1,2000)
	--if ret ~= 0 or runstatus ~= 4 then
		--Error("切换运行模式失败")
		--goto exit
	--end
	--设置推电流
	ret = client:WriteUInt16(slave_ID,1010,test_I_ref)	
	if ret == 0 then
		Info(string.format("设置推电流:%dmA",test_I_ref))
	else
		Error(string.format("设置推电流:%dmA失败",test_I_ref))
		goto exit
	end
	--设置速度
	
	--设置推动作
	ret = client:WriteUInt16(slave_ID,1003,1)	
	if ret ~= 0 then
		Error("设置推动作失败")
		goto exit
	end
	Delay(1500)
	--读电流参考
	ret,torque_ref = client:Read03FloatCDAB(slave_ID,114)
	if ret ~= 0 then
		Error("读电流参考失败")
		goto exit
	end
	Info(string.format("推电流参考:%fA",torque_ref))
	--读电流反馈
	ret,torque_fbk = client:Read03FloatCDAB(slave_ID,126)
	if ret ~= 0 then
		Error("读电流反馈失败")
		goto exit
	end
	Info(string.format("推电流反馈:%fA",torque_fbk))
	--读压力
	ret,torque_mes = client:Read04FloatABCD(99,0)
	if ret ~= 0 then
		ret,torque_mes = client:Read04FloatABCD(99,0)
		if ret ~= 0 then
			ret,torque_mes = client:Read04FloatABCD(99,0)
			if ret ~= 0 then
				Error("读压力测量失败")
				goto exit
			end
		end
	end
	torque_mes = torque_mes * 9.8
	Info(string.format("测量压力:%fN",torque_mes))
	--ret,force_fbk = client:ReadUInt16(1,2004)
	--if ret ~=0 then
	--	Error("读设备反馈压力失败")
	--	goto exit
	--end
	--Info(string.format("设备反馈压力:%dN",force_fbk))
	--保存数据
	writer:WriteFloat(-torque_ref)
	writer:WriteSplitter()
	writer:WriteFloat(-torque_fbk)
	writer:WriteSplitter()
	writer:WriteFloat(torque_mes)
	writer:WriteSplitter()
	--writer:WriteFloat(force_fbk)
	--writer:WriteSplitter()
	writer:WriteFloat(torque)
	writer:WriteSplitter()
	writer:WriteLine()
	Delay(100)
	--设置拉动作
	ret = client:WriteUInt16(slave_ID,1003,0)	
	if ret ~= 0 then
		Error("设置拉动作失败")
		goto exit
	end
	Delay(1000)
end

::exit:: 
writer:Close()
client:Disconnect()